package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class PermissoinServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        //通过拿到分页数据,然后进行sql查询
        Integer pageSize = pageBean.getPageSize();
        Integer currentPage = pageBean.getCurrentPage();
        String queryString = pageBean.getQueryString();
        //开启分页查询
        PageHelper.startPage(currentPage,pageSize);
        //通过分页查询得出所需要的参数
        Page<Permission> pages =permissionDao.findAllByPage(queryString);
        List <Permission> result = pages.getResult();
        long total = pages.getTotal();
        return new PageResult(total,result);
    }

    @Override
    public void addPermission(Map map) {
        //通过传过来3数据,进行权限的增加
        String name = (String) map.get("name");
        String keyword = (String) map.get("keyword");
        String description = (String) map.get("description");
        //把三个数据进行封装
        Permission permission=new Permission();
        permission.setName(name);
        permission.setDescription(description);
        permission.setKeyword(keyword);
        //把permission传递到dao
        permissionDao.addPermission(permission);
    }

    @Override
    public void deletePermission(String id) {
        //根据id删除
        permissionDao.deleteById(id);
    }

    @Override
    public void editPermission(Permission permission) {
        permissionDao.editPermission(permission);
    }

    @Override
    public Permission findById(String id) {
        Permission permission=permissionDao.findById(id);
        return permission;
    }
}
